import axios from 'axios';


const baseURL = 'http://j3d84376.natappfree.cc' //   http://localhost:8080  2q7yif.natappfree.cc

export const BaseURL = baseURL;
// 创建 Axios 实例
const apiClient1 = axios.create({
    baseURL: baseURL, // 替换为你的 API 基础 URL
    timeout: 10000, // 请求超时设置
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        // 'Authorization': localStorage.getItem('token') || ''
    },
});
// 创建 Axios 实例
const apiClient2 = axios.create({
    baseURL: baseURL, // 替换为你的 API 基础 URL
    timeout: 10000, // 请求超时设置
    headers: {
        'Content-Type': 'application/json',
        'Authorization': localStorage.getItem('token')
    },
});

// 添加请求拦截器
apiClient1.interceptors.request.use(config => {
    const token = localStorage.getItem('token');
    if (token) {
        config.headers['Authorization'] = token;
    }
    return config;
}, error => {
    return Promise.reject(error);
});
// 添加请求拦截器
apiClient2.interceptors.request.use(config => {
    const token = localStorage.getItem('token');
    if (token) {
        config.headers['Authorization'] = token;
    }
    return config;
}, error => {
    return Promise.reject(error);
});
// 封装 GET 请求函数
export const apiGet = async (endpoint, params = {}) => {
    try {
        const response = await apiClient1.get(endpoint, { params });
        console.log('GET:',endpoint,response.data);
        return response.data; // 返回数据部分
    } catch (error) {
        console.error('Error during GET request:', error);
        throw error; // 抛出错误以便调用者处理
    }
};

// 封装 POST 请求函数
export const apiPost = async (endpoint, data) => {
    console.log(data);
    try {
        const response = await apiClient2.post(endpoint, data);
        console.log('POST:',endpoint,response.data);
        return response.data; // 返回数据部分
    } catch (error) {
        console.error('Error during POST request:', error);
        throw error; // 抛出错误以便调用者处理
    }
};